package  {
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.events.Event;
	import org.papervision3d.materials.*;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.special.*;
	import org.papervision3d.materials.utils.MaterialsList;
	import CubeEvent;
	
	public class CubeMaker extends Sprite {
		
		private var _colorMaterial = new ColorMaterial(0x000000,1);
		private var _dcRef;
		private var _banners:Array;
		private var _cubeArray:Array = [];
		private var _cubeCoung:int = 0;
		private var _numberOfPieces:int;
		private var _width:Number;
		private var _depth:Number;
		private var _height:Number;
		
		public function CubeMaker(banners:Array, numberOfPieces:int) {
			
			_dcRef = DocumentClass._base;
			_banners = banners;
			_numberOfPieces = numberOfPieces;
		}
		
		public function makeCubes() {
			
			for (var i = 0; i < _numberOfPieces; i++) {
				var faceBitmapArray:Array;
				//fill array with four faces for current cube
				faceBitmapArray =  fillFace(i);
				
				//get width and height from a piece instance
				var width:Number;
				var height:Number;
				var tempArray:Array;
				tempArray = _banners[0];
				width = tempArray[0].width;
				_width = tempArray[0].width;
				height = tempArray[0].height;
				_height = tempArray[0].height;
				_depth = tempArray[0].height;
				var myCube:Cube;
				myCube = new Cube(new MaterialsList( { front:faceBitmapArray[0], back:faceBitmapArray[1], left:_colorMaterial, right:_colorMaterial, top:faceBitmapArray[2], bottom:faceBitmapArray[3] } ), _width, _height, _height, 10, 10, 10);
				var faces:MaterialsList = myCube.materials;
				trace("face front: " + faces.getMaterialByName("front").oneSide);
				trace("face back: " + faces.getMaterialByName("back").oneSide);
				trace("face top: " + faces.getMaterialByName("top").oneSide);
				trace("face bottom: " + faces.getMaterialByName("bottom").oneSide);
				_cubeArray[i] = myCube;
				if (i == (_numberOfPieces - 1)) cubesMade();
			}
		}
		private function fillFace(i:int):Array {
			var faceBitmapArray:Array = [];
			for (var j = 0; j < 4; j++) {
				//tempBannerArray filled with one banner in pieces
				var tempBannerArray:Array = _banners[j];
				//batmapData created and sized to current banner piece
				var bitmapData = new BitmapData(tempBannerArray[i].width, tempBannerArray[i].height);
				//bitmapData filled with current banner piece bitmap data
				bitmapData = tempBannerArray[i].bitmapData;
				//new bitmap matterial is created then filled witht hte bitmapData;
				var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmapData);
				//the array is filled with the current bitmapMaterial
				faceBitmapArray[j] = bitmapMaterial;
			}
			return faceBitmapArray;
		}
		
		private function cubesMade() {
			//dispatch event to notify of cube making completion
			dispatchEvent(new CubeEvent(CubeEvent.CUBES_MADE, _cubeArray.length));
		}
		
		public function get cubes():Array {
			return _cubeArray;
		}
		
		public function get cubeWidth():Number {
			return _width;
		}
		
		public function get cubeHeight():Number {
			return _height;
		}
		
		public function get cubeDepth():Number {
			return _depth;
		}
	}	
}